package com.dajun.result;

import com.github.pagehelper.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;


@Data
public class PageBean<T> implements Serializable {

    /**
     * 页码
     */
    private int pageNum;

    /**
     * 每页大小
     */
    private int pageSize;

    /**
     * 总记录数
     */
    private long total;

    /**
     * 总页数
     */
    private int pages;

    /**
     * 每页大小
     */
    private List<T> data;

    public PageBean(List<T> data) {
        this(data, 8);
    }

    public PageBean(List<T> data, int navigatePages) {
        if (data instanceof Page) {
            Page page = (Page) data;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.pages = page.getPages();
            this.data = page;
            this.total = page.getTotal();
        } else if (data instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = data.size();
            this.pages = this.pageSize > 0 ? 1 : 0;
            this.data = data;
            this.total = data.size();
        }

    }
}

